﻿using Newtonsoft.Json;
using NodaTime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ThresholdAssistant.Tools
{
    /// <summary>
    /// 数据类型转换帮助类
    /// </summary>
    public static class DataConvertUtil
    {
        /// <summary>
        /// 转换分页的大小
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sourceList"></param>
        /// <param name="pageSize">分页大小(默认：2000条)</param>
        /// <returns></returns>
        public static int ToPageCount<T>(this IEnumerable<T> sourceList, int pageSize = 2000)
        {
            if (sourceList == null || !sourceList.Any() || pageSize <= 0)
                return 0;

            return (int)Math.Ceiling((decimal)sourceList.Count() / pageSize);
        }

        /// <summary>
        /// 转换分页列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sourceList">数据源</param>
        /// <param name="pageIndex">分页页码</param>
        /// <param name="pageSize">分页大小</param>
        /// <returns></returns>
        public static IEnumerable<T> ToPageList<T>(this IEnumerable<T> sourceList, int pageIndex = 0, int pageSize = 2000)
        {
            if (sourceList == null || !sourceList.Any())
                return new List<T>();

            if (pageIndex <= 0)
                pageIndex = 1;

            return sourceList.Skip((pageIndex - 1) * pageSize).Take(pageSize);
        }

        /// <summary>
        /// 转换分页字典列表(Key存页码,Value存分页数据)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sourceList">数据源</param>
        /// <param name="pageSize">分页大小</param>
        /// <returns></returns>
        public static Dictionary<int, IEnumerable<T>> ToPageDictionary<T>(this IEnumerable<T> sourceList, int pageSize = 2000)
        {
            var dic = new Dictionary<int, IEnumerable<T>>();
            if (sourceList == null || !sourceList.Any())
                return dic;

            for (int pageIndex = 1; pageIndex <= sourceList.ToPageCount(pageSize); pageIndex++)
            {
                dic.Add(pageIndex, sourceList.ToPageList(pageIndex, pageSize));
            };
            return dic;
        }

        /// <summary>
        /// 字符串按指定长度换行
        /// </summary>
        /// <param name="content"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string NewLine(this string content, int length)
        {
            StringBuilder @string = new StringBuilder();
            for (int index = 0; index < content.Length; index++)
            {
                int r = index % length;
                int last = (content.Length / length) * length;
                if (index != 0 && index <= last)
                {
                    if (r == 0)
                    {
                        @string.Append(content.Substring(index - length, length) + "\n");
                    }
                }
                else if (index > last)
                {
                    @string.Append(content.Substring(index - 1));
                    break;
                }
            }
            return @string.ToString();
        }

        /// <summary>
        /// 截取left和right中间的内容
        /// </summary>
        /// <param name="content"></param>
        /// <param name="left"></param>
        /// <param name="right"></param>
        /// <returns></returns>
        public static string MidStr(this string content, string left, string right)
        {
            if (string.IsNullOrWhiteSpace(content))
            {
                return "";
            }
            int leftIndex = content.IndexOf(left);
            if (leftIndex > -1)
            {
                content = content.Substring(leftIndex + left.Length);
                int rightIndex = content.IndexOf(right);
                if (rightIndex > -1)
                {
                    return content.Substring(0, rightIndex);
                }
            }
            return "";
        }

        /// <summary>
        /// 字符串转换成对象
        /// </summary>
        /// <typeparam name="T">返回的泛型对象</typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public static T ToObject<T>(this string value)
        {
            T result = default;
            try
            {
                if (string.IsNullOrEmpty(value))
                {
                    return result;
                }
                result = (T)Convert.ChangeType(value, typeof(T));
            }
            catch
            {
            }
            return result;
        }

        public static T JsonToObj<T>(this string value)
        {
            T result = default;

            try
            {
                result = JsonConvert.DeserializeObject<T>(value);
            }
            catch { }

            return result;
        }

        public static string ToJsonStr(this object obj)
        {
            string result = string.Empty;

            try
            {
                result = System.Text.Json.JsonSerializer.Serialize(obj);
            }
            catch { }

            return result;
        }
    }
}